library(survival)
library(survminer)
surv_obj_mimic <- Surv(time = mimic_hosp_km$hospstay_30d,event = mimic_hosp_km$hospmort)
fit_mimic <- survfit(surv_obj_mimic~sad, data=mimic_hosp_km)
ggsurvplot(fit_mimic,
           conf.int = TRUE,
           risk.table = TRUE,
           pval = TRUE,
           ylim=c(0.4,1.0),
           ggtheme=theme_minimal()
           )
#比较组间生存差异的P值
library(survival)
library(survminer)
#outFile="survival 60d.pdf"
#groupnum <- length(levels(factor(mimic_sad_aki_km[,"sad"])))
diff <- survdiff(Surv(hospstay_30d,hosp_mort)~sad,data = mimic_sad_aki_km)
pvalue <- 1-pchisq(diff$chisq,df=(2-1))#df自由度
if(pvalue<0.001){
  pvalue="Log-rank test \n p<0.001"
}else{
  pvalue=paste0("Log-rank test","\n","p=",sprintf("%.03f",pvalue))
}
fit <- survfit(Surv(hospstay_30d,hosp_mort)~sad,data = mimic_sad_aki_km)
#绘制
mimic_hosp_30d <- ggsurvplot(fit,
                             data = mimic_sad_aki_km,
                             conf.int = F,
                             #pval = T,
                             pval = pvalue,
                             pval.size=5,
                             legend.labs=c("非谵妄组","谵妄组"),
                             #legend.labs=levels(factor(mimic_hosp_km[,"sad"])),
                             legend.title="组别",
                             xlab="Time(day)",
                             break.time.by=10,
                             risk.table.title="Number at risk",
                             risk.table = T,
                             risk.table.height=.25,
                             censor = T,
                             censor.shape=124,
                             censor.size=2,
                             ggtheme = theme_bw()
                             )
#pdf(file=outFile,onefile = FALSE,width = 15,height =8)
print(mimic_hosp_30d)
dev.off()